package com.tobby.camel.camelinaction.chapter3.transform;

import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class TransformByExpressionTest extends CamelTestSupport {

	@Override
	protected RouteBuilder createRouteBuilder() {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct:start").transform(new Expression() {

					@Override
					public <T> T evaluate(Exchange exchange, Class<T> type) {
						String body = exchange.getIn().getBody(String.class);
						body = body.replaceAll("\n", "<br/>");
						body = "<body>" + body + "</body>";
						return (T) body;
					}
				}).to("mock:result");
			}
		};
	}

	@Test
	public void testTransformExpression() throws InterruptedException {
		MockEndpoint mock = getMockEndpoint("mock:result");
		mock.expectedBodiesReceived("<body>Hello<br/>How are you?</body>");

		template.sendBody("direct:start", "Hello\nHow are you?");
		assertMockEndpointsSatisfied();
	}

}
